#pragma once
#include<vector>
#include<iostream>
#include<functional>

using namespace std;
namespace sl {
    template<class T>
    class less {
    public:
        bool operator()(const T& a, const T& b)
        {
            return a < b;
        }
    };
    template<class T>
    class less<T*>
    {
    public:
        bool operator()(const T* const& x, const T* const& y)
        {
            return *x < *y;
        }
    };
    template <class T, class Container = vector<T>, class Compare = less<T> >
    class priority_queue
    {
    public:
        priority_queue() {}
        template <class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
        {
            while (first != last)
            {
                c.push_back(*first);
                first++;
            }
            for (int i = (c.size() - 2) / 2; i >= 0; --i)
            {
                adjustdown(i);
            }
        }

        bool empty() const
        {
            return c.empty();
        }
        size_t size() const
        {
            return c.size();
        }
        const T& top() const
        {
            return c[0];
        }
        void push(const T& x)
        {
            c.push_back(x);
            adjustup(c.size() - 1);
        }
        void pop()
        {
            swap(c[0], c[c.size() - 1]);
            c.pop_back();
            adjustdown(0);
        }
    private:
        Container c;
        Compare comp;
    private:
        void adjustup(int child)
        {
            Compare com;
            size_t parent = (child - 1) / 2;
            while (child > 0)
            {
                if (com(c[parent], c[child]))
                {
                    swap(c[child], c[parent]);
                    child = parent;
                    parent = (child - 1) / 2;
                }
                else
                {
                    break;
                }
            }
        }
        void adjustdown(int parent)
        {
            Compare com;
            size_t child = parent * 2 + 1;
            while (child < c.size())
            {
                if (child + 1 < c.size() && com(c[child], c[child + 1]))
                {
                    child++;
                }
                if (com(c[parent], c[child]))
                {
                    swap(c[child], c[parent]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else
                {
                    break;
                }
            }
        }
    };
}